package com.mcptt.config.body;

import android.util.Xml;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.body.pidf.PidfXmlBody;

import org.xmlpull.v1.XmlPullParser;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class InXml {
//	private static final String End_Mcptt_Tag = "</IN_XML>";
	private static final String Name_Msg = "Msg";
	private static final String Name_SubMsg = "SubMsg";
	private static final String Name_McIds = "McIds";
	private static final String Name_McId = "McId";
//	private static final String Name_TriggerId = "TriggerId";

	private String msg = null;
	private String subMsg = null;
	private List<String> mcIdList = null;

	public InXml()
	{
	}


	public static InXml parseXmlStr(String xmlStr)
	{
		InXml body = new InXml();

		ByteArrayInputStream strIs = new  ByteArrayInputStream(xmlStr.getBytes());
		XmlPullParser pullParser = Xml.newPullParser();
		try {
			pullParser.setInput(strIs, "UTF-8"); //为Pull解释器设置要解析的XML数据
			int event = pullParser.getEventType();

			while (event != XmlPullParser.END_DOCUMENT){

				switch (event) {

					case XmlPullParser.START_DOCUMENT:
						break;
					case XmlPullParser.START_TAG:
						if (Name_Msg.equals(pullParser.getName()))
						{
							body.msg = pullParser.nextText();
						}
						else if (Name_SubMsg.equals(pullParser.getName()))
						{
							body.subMsg = pullParser.nextText();
						}
						else if (Name_McIds.equals(pullParser.getName()))
						{
							body.mcIdList = new Vector<>();
						}
						else if (Name_McId.equals(pullParser.getName()))
						{
							String mcid = pullParser.nextText();
							Log.debug(PidfXmlBody.class,"parseXmlStr mcid = " + mcid);
							body.mcIdList.add(mcid);
						}
						break;
					case XmlPullParser.END_TAG:
						break;

				}

				event = pullParser.next();
			}

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return body;
	}

	public String getMsg() {
		return msg;
	}
	public String getSubMsg() {
		return subMsg;
	}

	public String getSubMsgDesc() {
		if(subMsg != null) {
			StringBuffer strbuf = new StringBuffer(subMsg);
			Log.debug(this.getClass(), "getSubMsgDesc mcIdList = " + mcIdList);
			if (mcIdList != null
					&& mcIdList.size() > 0) {
				strbuf.append("(");
				for (int i = 0; i < mcIdList.size(); i++) {
					strbuf.append(mcIdList.get(i));
					if (i == mcIdList.size() - 1) {
						strbuf.append(")");
					} else {
						strbuf.append(",");
					}
				}
			}
			return strbuf.toString();
		}
		return "";
	}

	public String toString()
	{
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("msg = ").append(msg).append(",");
		strBuff.append("subMsg = ").append(subMsg).append(",");
		strBuff.append("mcIdList = ").append(mcIdList).append(".");

		return strBuff.toString();
	}
}
